package com.moujitx.pickpeach;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private Button gotoPick;

    private TextView peachText;
    private int totalCount = 0;
    Toast toast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        peachText = findViewById(R.id.peachText);
        gotoPick = findViewById(R.id.gotoPick);
        gotoPick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,pick.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("totalResult", String.valueOf(totalCount));
        if (requestCode == 1 && resultCode == 1) {
            int count = data.getIntExtra("count", 0);
            totalCount = totalCount + count;
            peachText.setText("总共摘到"+totalCount+"个桃子。");
            if(toast != null) toast.cancel();
            toast = Toast.makeText(MainActivity.this, "总共摘到" + totalCount + "个桃子", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}